/*
 * CreateZipFile.java
 *
 * Created on October 4, 2007, 10:55 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package consultasportletestadoordenproceso;

import com.src.log4j.logger.custom.XLogger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZipFile {
    
    private static XLogger xLogger;
    public static int BUFFER_SIZE = 10240;
    
    public void CreateZipFile() {
        this.xLogger = (XLogger) XLogger.getLogger(this.getClass().getName());
    }
    
    public void createZipArchive(File archiveFile, File[] tobeZippedFiles) {
        try {
            byte buffer[] = new byte[BUFFER_SIZE];
            FileOutputStream stream = new FileOutputStream(archiveFile);
            ZipOutputStream out = new ZipOutputStream(stream);
            for (int i = 0; i < tobeZippedFiles.length; i++) {
                if (tobeZippedFiles[i] == null || !tobeZippedFiles[i].exists() || tobeZippedFiles[i].isDirectory())
                    continue;
                ZipEntry zipAdd = new ZipEntry(tobeZippedFiles[i].getName());
                zipAdd.setTime(tobeZippedFiles[i].lastModified());
                out.putNextEntry(zipAdd);
                FileInputStream in = new FileInputStream(tobeZippedFiles[i]);
                while (true) {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if (nRead <= 0)
                        break;
                    out.write(buffer, 0, nRead);
                }
                in.close();
            }
            out.close();
            stream.close();
        }
        catch (Exception e) {
            xLogger.error("CreateZipFile:createZipArchive:", e);
        }
    }
    
    public void createZipArchive(File archiveFile, File tobeZippedFiles) {
        try {
            byte buffer[] = new byte[BUFFER_SIZE];
            FileOutputStream stream = new FileOutputStream(archiveFile);
            ZipOutputStream out = new ZipOutputStream(stream);
            if (tobeZippedFiles!= null && tobeZippedFiles.exists() && !tobeZippedFiles.isDirectory()){
                ZipEntry zipAdd = new ZipEntry(tobeZippedFiles.getName());
                zipAdd.setTime(tobeZippedFiles.lastModified());
                out.putNextEntry(zipAdd);
                FileInputStream in = new FileInputStream(tobeZippedFiles);
                while (true) {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if (nRead <= 0)
                        break;
                    out.write(buffer, 0, nRead);
                }
                in.close();
            }
            out.close();
            stream.close();
        }
        catch (Exception e) {
            xLogger.error("CreateZipFile:createZipArchive:", e);
        }
    }
}